import 'package:flutter/material.dart';
import 'package:flutter_wisave/eye/EyeDetailPage.dart';
import 'package:flutter_wisave/Tools/NetworkService.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class EyeItem extends StatelessWidget {
  final Map<String, dynamic> data;
  final Function? onRefresh;
  EyeItem({super.key, required this.data, this.onRefresh});

  final NetworkService _networkService = NetworkService();

  get context => null;

  Future<void> _loadData(bool add, int id) async {
    print(add);
    try {
      final result = await _fetchData(add, id);
      if (result['code'] == 200) {
        onRefresh?.call();
        if (add) {
          TDToast.showText('添加成功', context: context);
        } else {
          TDToast.showText('移除成功', context: context);
        }
      }
    } catch (e) {}
  }

  Future<Map<String, dynamic>> _fetchData(bool add, int id) async {
    if (add) {
      try {
        final response = await _networkService
            .get('/plcTimer/usermonitor/add?monitorId=$id');
        print('===========');
        if (response.statusCode == 200) {
          return response.data;
        } else {
          throw Exception('EyeItem Failed to load data');
        }
      } catch (e) {
        rethrow;
      }
    }
    try {
      final response = await _networkService.post(
        '/plcTimer/usermonitor/Delete?monitorId=$id',
      );
      if (response.statusCode == 200) {
        return response.data;
      } else {
        throw Exception('steeloutday Failed to load data');
      }
    } catch (e) {
      rethrow;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 0.0),
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/images/cloud/eye_bg.png'),
          fit: BoxFit.cover,
        ),
        borderRadius: BorderRadius.circular(8.0),
      ),
      child: GestureDetector(
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => EyeDetailPage(data: data)),
          );
        },
        child: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: NetworkImage(data['videoAddress']),
              fit: BoxFit.cover,
            ),
            borderRadius: BorderRadius.circular(8.0),
          ),
          child: Column(
            children: [
              SizedBox(height: 5),
              Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    GestureDetector(
                      onTap: () {
                        _loadData(data['isFavorited'] == 0, data['monitorId']);
                      },
                      child: Image(
                        image: AssetImage(data['isFavorited'] == 1
                            ? 'assets/images/cloud/eye_del.png'
                            : 'assets/images/cloud/eye_add.png'),
                        width: 30,
                        height: 30,
                      ),
                    ),
                    SizedBox(width: 10),
                  ],
                ),
              ),
              Spacer(),
              Container(
                child: Row(
                  children: [
                    SizedBox(width: 10),
                    Text(
                      data['monitorName'] ?? '',
                      style: const TextStyle(fontSize: 15, color: Colors.white),
                    ),
                  ],
                ),
              ),
              SizedBox(height: 5),
            ],
          ),
        ),
      ),
    );
  }
}
